<?php
namespace Duobao\Controller;


use Duobao\Model\OrderModel as Order;

class OrderController extends ControllerBase{
    /**
     * 默认
     */
    public function index()
    {
        echo "<!--hi , i'm from " . MODULE_NAME . "~" . APP_STATUS, "-->";
    }

    /**
     * 订单分配路由
     */
    public function createOrder(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $uid = $this->getUid(I('get.token'));
            $type = I('post.type');
            $data = $_POST['data'];#前端传递的是一个json串，不能使用I方法接受数据

            if(!empty($type)){
                if(!empty($data)){
                    if(is_numeric($uid)){
                        $model = new Order();
                        if($type == 'recharge'){
                            $order = $model->createRechargeOrder($uid,$data);#生成微信充值订单
                            $ret['data']['order'] = $order;
                        }elseif($type == 'balances' || $type == 'weixin'){
                            $data = json_decode($data,true);
                            #判断json的转换是否成功，如果不成功返回false
                            if($data){
                                $order = $model->CreateOrder($uid,$type,$data);#生成余额订单和微信订单
                                $ret['data']['order'] = $order;
                            }else{
                                $ret['status'] = 403;
                                $ret['msg'] = 'date format error';
                            }
                        }else{
                            $ret['data']['order'] = false;
                        }
                    }else{
                        $ret['status'] = 403;
                        $ret['msg'] = $uid;
                    }
                }else{
                    $ret['status'] = 403;
                    $ret['msg'] = 'empty of order data';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = 'empty of order type';
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }
    /* end of order controller */
}